<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\api\controller;

use app\service\BuyService;

use Think\Db;

/**
 * 购物车
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class Cart extends Common
{
    /**
     * [__construct 构造方法]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-03T12:39:08+0800
     */
    public function __construct()
    {
        // 调用父类前置方法
        parent::__construct();

        // 是否登录
        $this->IsLogin();
    }
    
    /**Arrivalreminder
     * [Index 首页]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-02-22T16:50:32+0800
     */
    public function Index()
    {
        return BuyService::CartList(['user'=>$this->user]);
    }

    /**
     * 购物车保存
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-13
     * @desc    description
     */
    public function Save()
    {
        $params = $this->data_post;
        $params['user'] = $this->user;
        return BuyService::CartAdd($params);
    }

    /**
     * 购物车删除
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-14
     * @desc    description
     */
    public function Delete()
    {
        $params = $this->data_post;
        $params['user'] = $this->user;
        return BuyService::CartDelete($params);
    }

    /**
     * 数量保存
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-14
     * @desc    description
     */
    public function Stock()
    {
        $params = $this->data_post;
        $params['user'] = $this->user;
        return BuyService::CartStock($params);
    }

    /*
     *到货提醒列表
     */
    public function ArrivalReminderlist(){
           // 参数
        $params = $this->data_request; 

     
        // 分页
        $number = isset($params['number'])? intval($params['number']):10;
        $page = max(1, isset($params['page']) ? intval($params['page']) : 1);

          // 获取总数
        $total = Db::name('arrival_reminder')->where(['user_id'=>$this->user['id']])->count();
        $page_total = ceil($total/$number);
        $start = intval(($page-1)*$number);


        $data=Db::name("arrival_reminder")->where(['user_id'=>$this->user['id']])->limit($start,$number)->select();

       

          // 返回数据
        $result = [
            'total'         =>  $total,
            'page_total'    =>  $page_total,
            'data'          =>  $data,
        ];
        return DataReturn('success', 0, $result);
    }


    /*
     *到货提醒
     */
    public function ArrivalReminder(){
        $params = $this->data_post;
        $params['user_id'] = $this->user['id'];

         // 请求参数
        $p = [
            
            [
                'checked_type' => 'empty',
                'key_name' => 'title',
                'error_msg' => '书名不能为空',
            ],         
            [
                'checked_type' => 'empty',
                'key_name' => 'user_id',
                'error_msg' => '用户信息有误',
            ],
        ];
        
        
        $ret = ParamsChecked($params, $p);
        if ($ret !== true) {
            return DataReturn($ret, -1);
        }

        $data=[
            'user_id'=>$params['user_id'],
            'goods_id'=>$params['goods_id'],
            'title'=>$params['title'],
            'images'=>empty($params['images'])?'':$params['images'],
            'add_time'=>time(),
            'isbn'=>$params['isbn'],           
        ];

        if(Db::name("ArrivalReminder")->insert($data)){
             return DataReturn('success', 0);
        }else{
             return DataReturn('error', -100);
        }

    }

    /*
     *取消到货提醒
     */
    public function CancelReminder(){
        $params = $this->data_post;
        $params['user_id'] = $this->user['id'];

         // 请求参数
        $p = [
            [
                'checked_type' => 'empty',
                'key_name' => 'id',
                'error_msg' => '到货提醒不能为空',
            ],
          
            [
                'checked_type' => 'empty',
                'key_name' => 'user_id',
                'error_msg' => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if ($ret !== true) {
            return DataReturn($ret, -1);
        }

       

        if(Db::name("ArrivalReminder")->where(['id'=> $params['id']])->delete()){
             return DataReturn('success', 0);
        }else{
             return DataReturn('error', -100);
        }

    }

}
?>